<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-2"></span><span class="hs-comment">{- |
   Module      :  System.Win32.Console.CtrlHandler
   Copyright   :  2008-2013 Judah Jacobson, 2013 shelarcy
   License     :  BSD-style

   Maintainer  :  shelarcy@gmail.com
   Stability   :  Provisional
   Portability :  Non-portable (Win32 API)

   Set handlers of console Ctrl events.
-}</span><span>
</span><span id="line-13"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">System.Win32.Console.CtrlHandler</span><span> </span><span>
</span><span id="line-14"></span><span>  </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="System.Win32.Console.html#CtrlEvent"><span class="hs-identifier">CtrlEvent</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#Handler"><span class="hs-identifier">Handler</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#PHANDLER_ROUTINE"><span class="hs-identifier">PHANDLER_ROUTINE</span></a></span><span>
</span><span id="line-15"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#withConsoleCtrlHandler"><span class="hs-identifier">withConsoleCtrlHandler</span></a></span><span>
</span><span id="line-16"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#setConsoleCtrlHandler"><span class="hs-identifier">setConsoleCtrlHandler</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#c_SetConsoleCtrlHandler"><span class="hs-identifier">c_SetConsoleCtrlHandler</span></a></span><span>
</span><span id="line-17"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#mkHandler"><span class="hs-identifier">mkHandler</span></a></span><span>
</span><span id="line-18"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.html#cTRL_C_EVENT"><span class="hs-identifier">cTRL_C_EVENT</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.html#cTRL_BREAK_EVENT"><span class="hs-identifier">cTRL_BREAK_EVENT</span></a></span><span>
</span><span id="line-19"></span><span>  </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Exception.html#"><span class="hs-identifier">Control.Exception</span></a></span><span>    </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/Control.Exception.Base.html#bracket"><span class="hs-identifier">bracket</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-22"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.html#"><span class="hs-identifier">Control.Monad</span></a></span><span>        </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/Data.Functor.html#void"><span class="hs-identifier">void</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-23"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Foreign.Ptr.html#"><span class="hs-identifier">Foreign.Ptr</span></a></span><span>          </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/GHC.Ptr.html#FunPtr"><span class="hs-identifier">FunPtr</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-24"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="System.Win32.Console.html"><span class="hs-identifier">System.Win32.Console</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="System.Win32.Console.html#CtrlEvent"><span class="hs-identifier">CtrlEvent</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.html#cTRL_C_EVENT"><span class="hs-identifier">cTRL_C_EVENT</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Console.html#cTRL_BREAK_EVENT"><span class="hs-identifier">cTRL_BREAK_EVENT</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-25"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="System.Win32.Types.html"><span class="hs-identifier">System.Win32.Types</span></a></span><span>   </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="System.Win32.Types.html#BOOL"><span class="hs-identifier">BOOL</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Win32.Types.html#failIfFalse_"><span class="hs-identifier">failIfFalse_</span></a></span><span> </span><span class="hs-special">)</span><span class="hs-cpp">

#include &quot;windows_cconv.h&quot;
</span><span>
</span><span id="line-29"></span><span class="hs-keyword">type</span><span> </span><span id="Handler"><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#Handler"><span class="hs-identifier hs-var">Handler</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="System.Win32.Console.html#CtrlEvent"><span class="hs-identifier hs-type">CtrlEvent</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="annot"><a href="System.Win32.Types.html#BOOL"><span class="hs-identifier hs-type">BOOL</span></a></span><span>
</span><span id="line-30"></span><span class="hs-comment">-- type HandlerRoutine = Handler</span><span>
</span><span id="line-31"></span><span class="hs-keyword">type</span><span> </span><span id="PHANDLER_ROUTINE"><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#PHANDLER_ROUTINE"><span class="hs-identifier hs-var">PHANDLER_ROUTINE</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="../../base/src/GHC.Ptr.html#FunPtr"><span class="hs-identifier hs-type">FunPtr</span></a></span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#Handler"><span class="hs-identifier hs-type">Handler</span></a></span><span>
</span><span id="line-32"></span><span>
</span><span id="line-33"></span><span id="local-6989586621679108240"><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#withConsoleCtrlHandler"><span class="hs-identifier hs-type">withConsoleCtrlHandler</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#Handler"><span class="hs-identifier hs-type">Handler</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="annot"><a href="#local-6989586621679108240"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="annot"><a href="#local-6989586621679108240"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-34"></span><span id="withConsoleCtrlHandler"><span class="annot"><span class="annottext">withConsoleCtrlHandler :: forall a. Handler -&gt; IO a -&gt; IO a
</span><a href="System.Win32.Console.CtrlHandler.html#withConsoleCtrlHandler"><span class="hs-identifier hs-var hs-var">withConsoleCtrlHandler</span></a></span></span><span> </span><span id="local-6989586621679108207"><span class="annot"><span class="annottext">Handler
</span><a href="#local-6989586621679108207"><span class="hs-identifier hs-var">handler</span></a></span></span><span> </span><span id="local-6989586621679108206"><span class="annot"><span class="annottext">IO a
</span><a href="#local-6989586621679108206"><span class="hs-identifier hs-var">io</span></a></span></span><span>
</span><span id="line-35"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO PHANDLER_ROUTINE
-&gt; (PHANDLER_ROUTINE -&gt; IO ())
-&gt; (PHANDLER_ROUTINE -&gt; IO a)
-&gt; IO a
forall a b c. IO a -&gt; (a -&gt; IO b) -&gt; (a -&gt; IO c) -&gt; IO c
</span><a href="../../base/src/Control.Exception.Base.html#bracket"><span class="hs-identifier hs-var">bracket</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-keyword">do</span><span> </span><span id="local-6989586621679108205"><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108205"><span class="hs-identifier hs-var">hd</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Handler -&gt; IO PHANDLER_ROUTINE
</span><a href="System.Win32.Console.CtrlHandler.html#mkHandler"><span class="hs-identifier hs-var">mkHandler</span></a></span><span> </span><span class="annot"><span class="annottext">Handler
</span><a href="#local-6989586621679108207"><span class="hs-identifier hs-var">handler</span></a></span><span>
</span><span id="line-36"></span><span>                </span><span class="hs-comment">-- don't fail if we can't set the Ctrl-C handler</span><span>
</span><span id="line-37"></span><span>                </span><span class="hs-comment">-- for example, we might not be attached to a console?</span><span>
</span><span id="line-38"></span><span>                </span><span class="annot"><span class="annottext">IO BOOL -&gt; IO ()
forall (f :: * -&gt; *) a. Functor f =&gt; f a -&gt; f ()
</span><a href="../../base/src/Data.Functor.html#void"><span class="hs-identifier hs-var">void</span></a></span><span> </span><span class="annot"><span class="annottext">(IO BOOL -&gt; IO ()) -&gt; IO BOOL -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE -&gt; BOOL -&gt; IO BOOL
</span><a href="System.Win32.Console.CtrlHandler.html#c_SetConsoleCtrlHandler"><span class="hs-identifier hs-var">c_SetConsoleCtrlHandler</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108205"><span class="hs-identifier hs-var">hd</span></a></span><span> </span><span class="annot"><span class="annottext">BOOL
</span><span class="hs-identifier hs-var">True</span></span><span>
</span><span id="line-39"></span><span>                </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE -&gt; IO PHANDLER_ROUTINE
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108205"><span class="hs-identifier hs-var">hd</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-40"></span><span>            </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679108204"><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108204"><span class="hs-identifier hs-var">hd</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IO BOOL -&gt; IO ()
forall (f :: * -&gt; *) a. Functor f =&gt; f a -&gt; f ()
</span><a href="../../base/src/Data.Functor.html#void"><span class="hs-identifier hs-var">void</span></a></span><span> </span><span class="annot"><span class="annottext">(IO BOOL -&gt; IO ()) -&gt; IO BOOL -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE -&gt; BOOL -&gt; IO BOOL
</span><a href="System.Win32.Console.CtrlHandler.html#c_SetConsoleCtrlHandler"><span class="hs-identifier hs-var">c_SetConsoleCtrlHandler</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108204"><span class="hs-identifier hs-var">hd</span></a></span><span> </span><span class="annot"><span class="annottext">BOOL
</span><span class="hs-identifier hs-var">False</span></span><span class="hs-special">)</span><span>
</span><span id="line-41"></span><span>            </span><span class="annot"><span class="annottext">((PHANDLER_ROUTINE -&gt; IO a) -&gt; IO a)
-&gt; (PHANDLER_ROUTINE -&gt; IO a) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">IO a -&gt; PHANDLER_ROUTINE -&gt; IO a
forall a b. a -&gt; b -&gt; a
</span><a href="../../base/src/GHC.Base.html#const"><span class="hs-identifier hs-var">const</span></a></span><span> </span><span class="annot"><span class="annottext">IO a
</span><a href="#local-6989586621679108206"><span class="hs-identifier hs-var">io</span></a></span><span>
</span><span id="line-42"></span><span>
</span><span id="line-43"></span><span class="hs-comment">-- | This function isn't suitable when we want to set the cTRL_C_EVENT handler.</span><span>
</span><span id="line-44"></span><span class="hs-comment">-- If you want to set the cTRL_C_EVENT handler, use 'c_SetConsoleCtrlHandler' instead.</span><span>
</span><span id="line-45"></span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#setConsoleCtrlHandler"><span class="hs-identifier hs-type">setConsoleCtrlHandler</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#PHANDLER_ROUTINE"><span class="hs-identifier hs-type">PHANDLER_ROUTINE</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="System.Win32.Types.html#BOOL"><span class="hs-identifier hs-type">BOOL</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-46"></span><span id="setConsoleCtrlHandler"><span class="annot"><span class="annottext">setConsoleCtrlHandler :: PHANDLER_ROUTINE -&gt; BOOL -&gt; IO ()
</span><a href="System.Win32.Console.CtrlHandler.html#setConsoleCtrlHandler"><span class="hs-identifier hs-var hs-var">setConsoleCtrlHandler</span></a></span></span><span> </span><span id="local-6989586621679108202"><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108202"><span class="hs-identifier hs-var">handler</span></a></span></span><span> </span><span id="local-6989586621679108201"><span class="annot"><span class="annottext">BOOL
</span><a href="#local-6989586621679108201"><span class="hs-identifier hs-var">flag</span></a></span></span><span>
</span><span id="line-47"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO BOOL -&gt; IO ()
</span><a href="System.Win32.Types.html#failIfFalse_"><span class="hs-identifier hs-var">failIfFalse_</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;SetConsoleCtrlHandler&quot;</span></span><span>
</span><span id="line-48"></span><span>      </span><span class="annot"><span class="annottext">(IO BOOL -&gt; IO ()) -&gt; IO BOOL -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE -&gt; BOOL -&gt; IO BOOL
</span><a href="System.Win32.Console.CtrlHandler.html#c_SetConsoleCtrlHandler"><span class="hs-identifier hs-var">c_SetConsoleCtrlHandler</span></a></span><span> </span><span class="annot"><span class="annottext">PHANDLER_ROUTINE
</span><a href="#local-6989586621679108202"><span class="hs-identifier hs-var">handler</span></a></span><span> </span><span class="annot"><span class="annottext">BOOL
</span><a href="#local-6989586621679108201"><span class="hs-identifier hs-var">flag</span></a></span><span>
</span><span id="line-49"></span><span>
</span><span id="line-50"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span id="mkHandler"></span><span class="hs-string">&quot;wrapper&quot;</span><span> </span><span class="hs-identifier">mkHandler</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Handler</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">PHANDLER_ROUTINE</span><span>
</span><span id="line-51"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;windows.h SetConsoleCtrlHandler&quot;</span><span>
</span><span id="line-52"></span><span>  </span><span id="c_SetConsoleCtrlHandler"><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#c_SetConsoleCtrlHandler"><span class="hs-identifier hs-var">c_SetConsoleCtrlHandler</span></a></span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="System.Win32.Console.CtrlHandler.html#PHANDLER_ROUTINE"><span class="hs-identifier hs-type">PHANDLER_ROUTINE</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="System.Win32.Types.html#BOOL"><span class="hs-identifier hs-type">BOOL</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="annot"><a href="System.Win32.Types.html#BOOL"><span class="hs-identifier hs-type">BOOL</span></a></span><span>
</span><span id="line-53"></span></pre></body></html>